//editables on first profile page
$.fn.editable.defaults.mode = 'inline';
$.fn.editableform.loading = "<div class='editableform-loading'><i class='light-blue icon-2x icon-spinner icon-spin'></i></div>";
$.fn.editableform.buttons = '<button type="submit" class="btn btn-info editable-submit"><i class="icon-ok icon-white"></i></button>' +
    '<button type="button" class="btn editable-cancel"><i class="icon-remove"></i></button>';

var merchantList;//代理机构列表
initMerchantSelect();
function initMerchantSelect() {
    $.ajax({
        type: "post",
        async: true,//异步，如果等于false 那么就是同步
        url: cochoApp.baseUrl + "/members/common/getMerchantInfo.do?token=" + cochoApp.getTokenByStorage(),
        dataType: "json",
        data: "",
        success: function (data) {
            if (data != null && data.statusCode == cochoApp.SUCCESS) {
                if (data.dataRows.length > 0) {
                    merchantList = data.dataRows;
                    for (var i = 0; i < merchantList.length; i++) {
                        var merchanInfo = merchantList[i];
                        if (merchanInfo != null) {
                            $("#merchantId").append("<option value='" + merchanInfo.merchantId + "'>" + merchanInfo.merchantName +  "</option>");
                        }
                    }
                    $("#merchantId").chosen();
                } else {
                    cochoAlert.notificationsWarn("暂无可选信息！");
                }
            } else {
                cochoAlert.notificationsWarn("暂时无法从服务器获取信息，请重新登录或联系管理员！");
            }
        },
        error: function (data) {
            cochoAlert.notificationsWarn(cochoErrorMessage.server_error_connect_admin);
        }
    });
}

function saveUserInfo() {
    var result = checkNullAndWarnHasNull();
    if (result == true) {//如果必填项都不为空
        var userId = $("#userId").val();
        var username = $("#userName").val();
        var userPassword = $("#userPassword").val();
        var mobilePhone = $("#mobilePhone").val();
        var merchantId = $("#merchantId").val();
        var roles = $("#roles").val();
        var status = $("#status").val();
        var paramData = {"userId":userId,"username": username,"userPassword": userPassword,"mobilePhone":mobilePhone,"merchantId":merchantId,"roles":roles,"status":status, "token": cochoApp.getTokenByStorage()};
        $.ajax({
            type: "post",
            async: true,//异步，如果等于false 那么就是同步
            url: cochoApp.baseUrl + "/admin/uc/saveUserInfo.do",
            dataType: "json",
            data: paramData,
            success: function (data) {
                if (data != null) {
                    cochoAlert.notificationsWarn(data.statusMsg);
                    $("#userId").val(data.content);
                } else {
                    cochoAlert.notificationsWarn("保存用户信息失败！");
                }
            },
            error: function (data) {
                cochoAlert.notificationsWarn(cochoErrorMessage.server_error_connect_admin);
            }
        });
    }
}

